#include "fileItemWidget.h"

FileItemWidget::FileItemWidget(QWidget* parent){
    this->setParent(parent);
    _text = "";
    _layout = new QHBoxLayout(this);
    _button_icon = new QPushButton(this);
    _button_text = new QPushButton(_text, this);
    _button_operate = new QPushButton(this);
    init();
}

FileItemWidget::FileItemWidget(QString text, QWidget* parent){
    this->setParent(parent);
    this->setStyleSheet("border: 0px");
    _text = text;
    _layout = new QHBoxLayout(this);
    _button_icon = new QPushButton(this);
    _button_text = new QPushButton(_text, this);
    _button_operate = new QPushButton(this);
    init();
}

QString FileItemWidget::getText(){
    return _text;
}
void FileItemWidget::setText(QString text){
    _text = text;
    if(text.contains("/")){
        _type = FOLDER;
    }else{
        _type = FILE;
    }
    _button_text->setText(text.remove("/"));
    refStyle();
}

void FileItemWidget::setFileType(FileType type){
    _type = type;
    refStyle();
}

FileItemWidget::FileType FileItemWidget::getFileType(){
    return _type;
}


void FileItemWidget::init(){


    _button_icon->setFixedSize(QSize(40,40));
    _button_text->setFixedHeight(40);
    _button_text->setMinimumWidth(200);
    _button_operate->setFixedSize(QSize(40,40));

    _button_icon->setStyleSheet("QPushButton{border:0; background-image: url(:/pic/picture/file.png)}");
    _button_text->setStyleSheet("background: #FFFFFF; border:0px; text-align:left;");
    _button_text->setStyleSheet("QPushButton{text-align: left;background: #FFFFFF; border:0px;}");

    _button_operate->setStyleSheet("QPushButton{border:0;background-image: url(:/pic/picture/delete.png)}");

    this->setLayout(_layout);
    _layout->setSpacing(10);
    _layout->addWidget(_button_icon);
    _layout->addWidget(_button_text);
    _layout->addWidget(_button_operate);
    _layout->addSpacerItem(new QSpacerItem(40, 40, QSizePolicy::Expanding));

    connect(_button_icon, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
    connect(_button_text, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
    connect(_button_operate, SIGNAL(clicked()), this, SLOT(on_operate_clicked()));

}

void FileItemWidget::refStyle(){
    switch (_type) {
    case FILE:
        _button_icon->setStyleSheet("QPushButton{border:0;background-image: url(:/pic/picture/file.png)}");
        _button_operate->hide();
        break;
    case FOLDER:
        _button_icon->setStyleSheet("QPushButton{border:0;background-image: url(:/pic/picture/folder.png)}");
        _button_operate->hide();
        break;
    case USB:
        _button_icon->setStyleSheet("QPushButton{border:0;background-image: url(:/pic/picture/usb2.png)}");
        _button_operate->hide();
        break;
    case IP:
        _button_icon->setStyleSheet("QPushButton{border:0;background-image: url(:/pic/picture/ip.png)}");
        _button_operate->show();
        break;

    }
}


void FileItemWidget::on_button_clicked(){
    emit sig_FileItemClicked(_text, _type);
}

void FileItemWidget::on_operate_clicked(){
    emit sig_FileItemOperate(_text);
}

